//
//  HXDDiscoverSearchView.swift
//  weibo
//
//  Created by huxuedong on 15/12/5.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

class HXDDiscoverSearchView: UIView, UITextFieldDelegate {

    @IBOutlet weak var textfield: UITextField!
    @IBOutlet weak var rightLayout: NSLayoutConstraint!
    @IBOutlet weak var cancelButton: UIButton!
    
    class func discoverSearchView () -> HXDDiscoverSearchView {
        return NSBundle.mainBundle().loadNibNamed("HXDDiscoverSearchView", owner: nil, options: nil).last! as! HXDDiscoverSearchView
    }
    
//    override init(frame: CGRect) {
//        //代码创建控件，重写该方法必须默认添加下面方法
//    }
    
//    required init?(coder aDecoder: NSCoder) {
//        //通过代码往当前控件添加子控件
//    }
    
    override func awakeFromNib() {
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.frame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
        leftView.contentMode = .Center
        textfield.leftView = leftView
        textfield.leftViewMode = .Always
    }

    @IBAction func cancelAction() {
        textfield.resignFirstResponder()
        self.rightLayout.constant = 0
        UIView.animateWithDuration(1) { () -> Void in
            self.textfield.layoutIfNeeded()
        }
    }
    
    //实现代理方法，当开始编辑的时候调用
    func textFieldDidBeginEditing(textField: UITextField) {
        self.rightLayout.constant = self.cancelButton.frame.width
        UIView.animateWithDuration(1) { () -> Void in
            self.textfield.layoutIfNeeded()
        }
    }
    
}
